<%@page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.io.*, java.util.*, javax.mail.*, javax.mail.internet.*, javax.activation.*" %>
<%
String host=(String)session.getAttribute("host");
String sender=(String)session.getAttribute("sender");
String senderpwd=(String)session.getAttribute("senderpwd");
if(host==null)host="10.200.16.33";
//if(sender==null)sender = "EPrice@cpimex.com";
if(sender==null)sender = "appmail";
if(senderpwd==null)senderpwd = "Sistemas1";



String recipient="dario.arellano@cpimex.com";
String subject="Contactanos";
String maildata="";
String errormsg="";
String send = request.getParameter("send");
String nombre = request.getParameter("name");
String mail = request.getParameter("mail");
String emp = request.getParameter("emp");
String ciudad = request.getParameter("ciudad");
String pais = request.getParameter("pais");
String coment = request.getParameter("coment");
boolean abort = false;
String msg = "";

if(nombre==null || nombre.length()<2)
{nombre=""; abort = true; msg += "el Nombre, ";}
if(mail==null || mail.length()<2)
{mail=""; abort = true; msg += "el E-mail, ";}
if(emp==null || emp.length()<2)
{emp=""; abort = true; msg += "la Empresa, ";}
if(ciudad==null || ciudad.length()<2)
{ciudad="Guadalajara";}
if(pais==null || pais.length()<2)
{pais="Mexico";}
if(coment==null || coment.length()<2)
{coment=""; abort = true; msg += "el Comentario, ";}

if(send!=null && !abort){
	   try    {////hace la estructura del mensaje
       maildata =/////es el texto del mensaje
            "<html><body>  \r\n" +
            "<b>Nombre:</b> " + nombre + "<br>\r\n" +
            "<b>E-mail:</b> " + mail + "<br>\r\n" +
            "<b>Empresa:</b> " + emp + "<br>\r\n" +
            "<b>Ciudad:</b> " + ciudad + "<br>\r\n" +
            "<b>Pa&iacute;s:</b> " + pais + "<br>\r\n" +
            "<b>Comentarios:</b><br> \r\n" + coment + "<br>\r\n\r\n" +
             "</body></html>" +
             "\r\n";

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	    // Get system properties
	    Properties props = System.getProperties();
	    // Setup mail server
	    props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true" );

	    // Get session
	    //Session sesion = Session.getInstance(props, null);

		final String mailFrom = sender;
		final String passwordFrom = senderpwd;
		Session sesion = Session.getInstance(props, new Authenticator() {
				public PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication( mailFrom, passwordFrom );
				}
			}
		);

	    // Define message
	    Message message = new MimeMessage(sesion);
	    message.setHeader("Content-Type", "text/html");
	    //message.setFrom(new InternetAddress(nombre+sender));
	    message.setFrom(new InternetAddress(nombre+"<"+mailFrom+">"));
	    InternetAddress[] ReplyAddress = { new InternetAddress(mail)};  
	    message.setReplyTo(ReplyAddress);
	    message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
	    message.setSubject(subject);
	    // Create the message part 
	    BodyPart messageBodyPart = new MimeBodyPart();
	    // Fill the message
 	    messageBodyPart.setText(maildata);
	    //Set the content type of message
	    messageBodyPart.setHeader("Content-Type","text/html");
	    // Create a Multipart
	    Multipart multipart = new MimeMultipart();
	    // Add part one
	    multipart.addBodyPart(messageBodyPart);
	    // Put parts in message
	    message.setContent(multipart);
	    // Send the message
	    Transport.send(message);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		response.sendRedirect("MailPrueba.jsp");
		return;
       }
	   catch(Exception e) { errormsg = "Error de: "+ e + ".";}


} 
if(send!=null && abort) {
msg = "Falta " + msg + " por favor llene todos los campos.";
} else msg="";

	Calendar d = Calendar.getInstance();
	int mes = d.get(d.MONTH)+1;
	int anio = d.get(d.YEAR); 
	int dia = d.get(d.DATE);
	String fecha = ""+anio+"-"+(mes<10?"0"+mes:""+mes)+"-"+(dia<10?"0"+dia:""+dia);		
	int hora = d.get(d.HOUR_OF_DAY);
	
%>
<html>
<head>
<script>
parent.frames['leftFrame'].location.replace('menu.jsp?param=contacto');
</script>
<title>Cont&aacute;ctanos</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#012788" link="#012788" vlink="#012788" alink="#012788">
<center>
  <form name="contacto" method="get" action="MailPrueba.jsp">
    <table border="0" width="328" cellspacing="3" cellpadding="3" bgcolor="#FFFFFF" bordercolor="#CCCCCC">
      <tr> 
        <td width="15">&nbsp;</td>
        <td colspan="2" bgcolor="#012788" > 
          <center>
            <font face="Arial, Helvetica, sans-serif" size="2"><b><font color="#FFFFFF">Sus 
            comentarios son bienvenidos</font></b></font> 
          </center>
        </td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td colspan="2"><font color="#DB2424" face="Arial, Helvetica, sans-serif" size="2"><%=msg%></font></td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td width="105"> 
          <div align="right"><b><font face="Arial, Helvetica, sans-serif" size="2">Nombre: 
            </font></b></div>
        </td>
        <td width="180"> 
          <input type="text" name="name" size="30" value="<%=nombre%>">
        </td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td width="105"> 
          <div align="right"><b><font face="Arial, Helvetica, sans-serif" size="2">E-mail: 
            </font></b></div>
        </td>
        <td width="180"> 
          <input type="text" name="mail" size="30" value="<%=mail%>">
        </td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td width="105"> 
          <div align="right"><b><font face="Arial, Helvetica, sans-serif" size="2">Empresa: 
            </font></b></div>
        </td>
        <td width="180"> 
          <input type="text" name="emp" size="30" value="<%=emp%>">
        </td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td width="105"> 
          <div align="right"><b><font face="Arial, Helvetica, sans-serif" size="2">Ciudad: 
            </font></b></div>
        </td>
        <td width="180"> 
          <input type="text" name="ciudad" size="30" value="<%=ciudad%>">
        </td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td width="105"> 
          <div align="right"><b><font face="Arial, Helvetica, sans-serif" size="2">Pa&iacute;s: 
            </font></b></div>
        </td>
        <td width="180"> 
          <input type="text" name="pais" size="30" value="<%=pais%>">
        </td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td width="105"> 
          <div align="right"><b><font face="Arial, Helvetica, sans-serif" size="2">Comentarios: 
            </font></b></div>
        </td>
        <td width="180">&nbsp; </td>
        <td width="15"></td>
      </tr>
      <tr> 
        <td width="15">&nbsp;</td>
        <td colspan="2" align="center"> 
          <textarea name="coment" cols="40" rows="5"><%=coment%></textarea> </td>
	  <td width="15"></td></tr> <tr> <td width="15">&nbsp;</td>
	  <td width="105"> <input type="hidden" name="send" value="true"> </td>
	  <td width="180"> 
</td>
<td width="15">
</td>
</tr> 
<tr> <td width="15">&nbsp;</td>
<td colspan="2"> <CENTER>
<a href="javascript:document.contacto.submit()" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image1','','images/enviar2.jpg',1)"><img src="images/enviar.jpg" width=120 height=23 alt="carrito" border="0" name="Image1"></a> 
&nbsp;&nbsp;&nbsp;
<a href="javascript:document.contacto.reset()" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Image3','','images/borrar2.jpg',1)"><img src="images/borrar.jpg" width=120 height=23 alt="carrito" border="0" name="Image3"></a> 
</CENTER></td><td width="15"></td></tr> </table><br> <%=errormsg%><br>
  </form>
  </center>
</body>
</html>
